package org.jks.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;
import org.jks.report.model.ReportUserModel;
import org.jks.util.DateUtil;
import org.jks.util.PrintUtil;

import com.google.gson.annotations.Expose;

@Entity
@Table(name = "User")
public class User extends BaseModel {
	public static final int SEX_MAN = 1;
	public static final int SEX_WOMAN = 2;
	@Id
	@GeneratedValue(generator = "increment")
	@GenericGenerator(name = "increment", strategy = "increment")
	@Expose
	private long Id;
	@Column(length = 64, nullable = false, unique=true)
	@Expose
	private String Username;
	@Column(length = 128, nullable = false)
	@Expose
	private String Password;
	@Column(length = 32, nullable = false)
	@Expose
	private String Name;
	/** 1: 男 2: 女 */
	@Expose
	private byte Sex;
	@ManyToOne
	@JoinColumn(name = "Flight", referencedColumnName = "Id")
	@Expose
	private Flight Flight;
	@ManyToOne
	@JoinColumn(name = "Department", referencedColumnName = "Id")
	@Expose
	private Department Department;
	@ManyToOne
	@JoinColumn(name = "Position", referencedColumnName = "Id")
	@Expose
	private Position Position;
	@ManyToOne
	@JoinColumn(name = "Education", referencedColumnName = "Id")
	@Expose
	private Education Education;
//	@Column(length = 256)
//	@Expose
//	private String RFID;
	@Column(length = 32)
	@Expose
	private String DocumentNumber;
	@Column(length = 256)
	@Expose
	private String TwoInchPhotos;
	@Column(length = 256)
	@Expose
	private String Recruitment;
	@Column(length = 256)
	@Expose
	private String HomeAddress;
	@Temporal(TemporalType.DATE)
	@Expose
	private Date ToPartInTheWorkTime;
	@Column(length = 13)
	@Expose
	private String ContactNumber;
	@Temporal(TemporalType.DATE)
	@Expose
	private Date Birthday;
	@ManyToOne
	@JoinColumn(name = "PoliticalStatus", referencedColumnName = "Id")
	@Expose
	private PoliticalStatus PoliticalStatus;
	@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.REFRESH })
	@JoinTable(name = "UserRole", joinColumns = @JoinColumn(name = "UserId"), inverseJoinColumns = @JoinColumn(name = "RoleId"))
	@Expose
	private Set<Role> roles = new HashSet<Role>();

	public long getId() {
		return Id;
	}

	public void setId(long id) {
		Id = id;
	}

	public String getUsername() {
		return Username;
	}

	public void setUsername(String username) {
		Username = username;
	}

	public String getPassword() {
		return Password;
	}

	public void setPassword(String password) {
		Password = password;
	}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public byte getSex() {
		return Sex;
	}

	public void setSex(byte sex) {
		Sex = sex;
	}

	public Flight getFlight() {
		return Flight;
	}

	public void setFlight(Flight flight) {
		Flight = flight;
	}

	public Department getDepartment() {
		return Department;
	}

	public void setDepartment(Department department) {
		Department = department;
	}

	public Position getPosition() {
		return Position;
	}

	public void setPosition(Position position) {
		Position = position;
	}

	public Education getEducation() {
		return Education;
	}

	public void setEducation(Education education) {
		Education = education;
	}

//	public String getRFID() {
//		return RFID;
//	}
//
//	public void setRFID(String rFID) {
//		RFID = rFID;
//	}

	public String getDocumentNumber() {
		return DocumentNumber;
	}

	public void setDocumentNumber(String documentNumber) {
		DocumentNumber = documentNumber;
	}

	public String getTwoInchPhotos() {
		return TwoInchPhotos;
	}

	public void setTwoInchPhotos(String twoInchPhotos) {
		TwoInchPhotos = twoInchPhotos;
	}

	public String getRecruitment() {
		return Recruitment;
	}

	public void setRecruitment(String recruitment) {
		Recruitment = recruitment;
	}

	public String getHomeAddress() {
		return HomeAddress;
	}

	public void setHomeAddress(String homeAddress) {
		HomeAddress = homeAddress;
	}

	public Date getToPartInTheWorkTime() {
		return ToPartInTheWorkTime;
	}

	public void setToPartInTheWorkTime(Date toPartInTheWorkTime) {
		ToPartInTheWorkTime = toPartInTheWorkTime;
	}

	public String getContactNumber() {
		return ContactNumber;
	}

	public void setContactNumber(String contactNumber) {
		ContactNumber = contactNumber;
	}

	public Date getBirthday() {
		return Birthday;
	}

	public void setBirthday(Date birthday) {
		Birthday = birthday;
	}

	public PoliticalStatus getPoliticalStatus() {
		return PoliticalStatus;
	}

	public void setPoliticalStatus(PoliticalStatus politicalStatus) {
		PoliticalStatus = politicalStatus;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	
	public ReportUserModel toReportModel(){
		ReportUserModel rum = new ReportUserModel();
		rum.setId(this.Id+"");
		rum.setUsername(this.Username);
		rum.setName(this.Name);
		if(this.Sex == SEX_MAN){
			rum.setSex("男");
		}
		else if(this.Sex == SEX_WOMAN){
			rum.setSex("女");
		}
		if(this.Department != null){
			rum.setDepartment(this.Department.getName());
		}
		else{
			rum.setDepartment("");
		}
		if(this.Position != null){
			rum.setPosition(this.Position.getName());
		}
		else{
			rum.setPosition("");
		}
		if(this.Education != null){
			rum.setEducation(this.Education.getName());
		}
		else{
			rum.setEducation("");
		}
		if(this.Flight != null){
			rum.setFlight(this.Flight.getName());
		}
		else{
			rum.setFlight("");
		}
		if(this.PoliticalStatus != null){
			rum.setPoliticalStatus(this.PoliticalStatus.getName());
		}
		else{
			rum.setPoliticalStatus("");
		}
//		if(this.RFID != null){
//			rum.setRFID(this.RFID);
//		}
//		else{
//			rum.setRFID("");
//		}
		if(this.DocumentNumber != null){
			rum.setDocumentNumber(this.DocumentNumber);
		}
		else{
			rum.setDocumentNumber("");
		}
		if(this.Recruitment!=null){
			rum.setRecruitment(this.Recruitment);
		}
		else{
			rum.setRecruitment("");
		}
		if(this.HomeAddress != null){
			rum.setHomeAddress(this.HomeAddress);
		}
		else{
			rum.setHomeAddress("");
		}
		if(this.ToPartInTheWorkTime != null){
			rum.setToPartInTheWorkTime(DateUtil.toString(this.ToPartInTheWorkTime, "yyyy-MM-dd"));
		}
		else{
			rum.setToPartInTheWorkTime("");
		}
		if(this.Birthday != null){
			rum.setBirthday(DateUtil.toString(this.Birthday, "yyyy-MM-dd"));
		}
		else{
			rum.setBirthday("");
		}
		if(this.ContactNumber != null){
			rum.setContactNumber(this.ContactNumber);
		}
		else{
			rum.setContactNumber("");
		}
		return rum;
	}

	@Override
	public String toString() {
		return "[Id:" + this.Id + ",Username:" + this.Username + ",Name:"
				+ this.Name + "," + "\nDepartment:" + this.Department
				+ ",\nPosition:" + this.Position + ",\nEducation:"
				+ this.Education + /*",RFID:" + this.RFID +*/ ",DocumentNumber:"
				+ this.DocumentNumber + "," + "TwoInchPhotos:"
				+ this.TwoInchPhotos + ",Recruitment:" + this.Recruitment + ","
				+ "HomeAddress:" + this.HomeAddress + ",ToPartInTheWorkTime:"
				+ this.ToPartInTheWorkTime + ",ContactNumber:"
				+ this.ContactNumber + ",Birthday:" + this.Birthday
				+ ",\nPoliticalStatus:" + this.PoliticalStatus + ",\nroles:"
				+ PrintUtil.toString(this.roles) + "]";
	}
}
